package activiti;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

public class Demo1 {
	@Test
	public void create_table(){
		//创建流程引擎配置对象
		ProcessEngineConfiguration cfg = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
		//配置数据库驱动
		cfg.setJdbcDriver("com.mysql.jdbc.Driver");
		cfg.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/activiti_01");
		cfg.setJdbcUsername("root");
		cfg.setJdbcPassword("123");
		//设置动为自动建表策略
		cfg.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
		//根据配置,创建流程引擎
		ProcessEngine processEngine = cfg.buildProcessEngine();
	}
	@Test
	public void create_table2(){
		ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml").buildProcessEngine();
	}
	
	@Test
	public void create_talbe3(){
		ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	}
	/**
	 * 部署流程定义
	 */
	ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();//默认流程生成表,获得流程引擎
	@Test
	public void deployment(){
		DeploymentBuilder deployment = processEngine.getRepositoryService().createDeployment();//先获得静态服务,在创建部署对象
		//加载流程文件
		deployment.addClasspathResource("qjlc.bpmn");
		deployment.addClasspathResource("qjlc.png");
		//部署流程对象
		Deployment deploy = deployment.deploy();
		System.out.println(deploy.getId());
	}
	
	/**
	 * 查看部署流程定义
	 */
	@Test
	public void queryDeploy(){
		ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
		//根据流程定义key过滤
		ProcessDefinitionQuery key = query.processDefinitionKey("qjlc");
		//添加排序条件
		query.orderByProcessDefinitionVersion().desc();
		//遍历,打印
		List<ProcessDefinition> list = query.list();
		for (ProcessDefinition pd : list) {
			System.out.println(pd.getDeploymentId()+"..."+pd.getId()+"..."+pd.getName()+"..."+pd.getVersion());
		}
	}
	/**
	 * 启动流程实例---根流程定义id
	 */
	//数据保存在execution中,打印的id即为execution的id
	@Test
	public void startProcessByDefId(){
		ProcessInstance startProcessInstanceById = 
				processEngine.getRuntimeService().startProcessInstanceById("qjlc:1:4");
		System.out.println(startProcessInstanceById);
	}
	
	/**
	 * 启动流程实例----根据流程定义key(),是每个流程独有的,所以通过key启动流程会自动选择最新版本的流程定义id启动
	 */
	public void startProcessByKey(){
		processEngine.getRuntimeService().startProcessInstanceByKey("qjlc");
	}
	
	/**
	 * 查询个人任务(待处理任务)
	 */
	@Test
	public void task(){
		TaskQuery taskQuery = processEngine.getTaskService().createTaskQuery();
		//根据任务人过滤
		taskQuery.taskAssignee("王五");
		List<Task> list = taskQuery.list();
		for (Task task : list) {
			System.out.println(task.getAssignee() +"..."+ task.getId()+"..."+task.getName());
		}
	}
	
	/**
	 * 处理个人任务--根据taskid
	 */
	@Test
	public void dealTask(){
		processEngine.getTaskService().complete("402");
	}
	/**
	 * 部署流程查询
	 */
	@Test
	public void showDeploy(){
		DeploymentQuery deploymentQuery = processEngine.getRepositoryService().createDeploymentQuery();
		List<Deployment> list = deploymentQuery.list();
		for (Deployment deployment : list) {
			System.out.println(deployment.getId()+"..."+deployment.getName()+"..."+deployment.getDeploymentTime());
		}
	}
}
